const app = new Vue({
  el: '#app',
  data: {
    baseImg: '',
    mobile: '',
    captchaKey: '',
    captchaCode: '',
    smsCode: '',
    timeTip: '获取验证码',
    timeOut: ''
  },
  created() {
    this.getCaptcha()
  },
  methods: {
    // 获取 图形验证码
    async getCaptcha() {
      const { data: res } = await axios.get('/captcha/image')
      //   console.log(res)
      //   图形验证码
      this.baseImg = res.data.base64
      //   保存图片的key
      this.captchaKey = res.data.key
      //   localStorage.setItem('key', JSON.stringify(res.data.key))
    },
    //   切换图形验证码
    change() {
      this.getCaptcha()
    },

    //   获取短信验证码
    async getMsg() {
      // 判断
      if (!this.mobile) return tip('请输入手机号')
      if (this.mobile.length !== 11) return tip('请输入正确手机号')
      // 倒计时禁止按钮点击
      this.timeOut = 5
      this.timeTip = '秒重新获取'
      this.timer = setInterval(() => {
        this.timeOut--
        if (this.timeOut <= 0) {
          clearInterval(this.timer)
          this.timeTip = '获取验证码'
          this.timeOut = ''
        }
      }, 1000)
      // 获取数据
      const { data: res } = await axios.post('/captcha/sendSmsCaptcha', {
        form: {
          captchaCode: this.captchaCode,
          captchaKey: this.captchaKey,
          mobile: this.mobile
        }
      })
      tip(res.message)
      console.log(res)
    },

    // 登录
    async getLogin() {
      if (!this.smsCode) return tip('验证码不能空')
      const { data: res } = await axios.post('/passport/login', {
        form: {
          isParty: false,
          mobile: this.mobile,
          partyData: {},
          smsCode: this.smsCode
        }
      })

      tip(res.message)
      console.log(res)
    }
  }
})
